package com.handu.luna.core.domain.goods.goodsMatch;

import com.handu.luna.core.domain.BasePersistentDomain;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.persistence.criteria.*;
import java.util.Collection;
import java.util.List;

/**
 * Created by 123 on 2014/7/22.
 */
@Entity
public class GoodsMatch extends BasePersistentDomain<GoodsMatchRepository>{

    public enum CasCadeType{
        ONEWAY("单向关联"),TWOWAY("双向关联");
        public String description;

        private CasCadeType(String description){
            this.description = description;
        }

    }

    @Transient private CasCadeType casCadeType = CasCadeType.ONEWAY;

    private long primaryGoodsId;

    private long cascadGoodsId;

    @Transient private String matchGoodsIds;

    @Transient
    private String primaryGoodsName;


    public CasCadeType getCasCadeType() {
        return casCadeType;
    }

    public void setCasCadeType(CasCadeType casCadeType) {
        this.casCadeType = casCadeType;
    }

    public long getPrimaryGoodsId() {
        return primaryGoodsId;
    }

    public void setPrimaryGoodsId(long primaryGoodsId) {
        this.primaryGoodsId = primaryGoodsId;
    }


    public GoodsMatch(long primaryGoodsId,long cascadGoodsId) {
        this.primaryGoodsId = primaryGoodsId;
        this.cascadGoodsId = cascadGoodsId;
    }
    public GoodsMatch(long primaryGoodsId) {
        this.primaryGoodsId = primaryGoodsId;
    }
    public GoodsMatch(){
        super();
    }

    public String getPrimaryGoodsName() {
        if (!StringUtils.isNotBlank(primaryGoodsName)){
            primaryGoodsName = context.getGoodsRepository().findOne(primaryGoodsId).getName();
        }
        return primaryGoodsName;
    }

    public void setPrimaryGoodsName(String primaryGoodsName) {
        this.primaryGoodsName = primaryGoodsName;
    }

    public long getCascadGoodsId() {
        return cascadGoodsId;
    }

    public void setCascadGoodsId(long cascadGoodsId) {
        this.cascadGoodsId = cascadGoodsId;
    }

    public String getMatchGoodsIds() {
        return matchGoodsIds;
    }

    public void setMatchGoodsIds(String matchGoodsIds) {
        this.matchGoodsIds = matchGoodsIds;
    }

    public Specification<GoodsMatch> searchByPidAndMid(Long pid,Collection<Long> coll){
        return new Specification<GoodsMatch>() {
            @Override
            public Predicate toPredicate(Root<GoodsMatch> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                List<Expression<Boolean>> expressions = predicate.getExpressions();
                expressions.add(cb.equal(root.<Long>get("primaryGoodsId"),pid));
                expressions.add(root.get("cascadGoodsId").in(coll));

                return query.where(predicate).getRestriction();
            }
        };
    }

}
